package com.ygo.feihua;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.ygo.feihua.util.*;

public class ShezhiEzmanagement extends AppCompatActivity implements OnClickListener
{

	private Toolbar tool;
	private Gongju gj;
	private TextView sm_zdy_ygopath,sm_zdy_cdbpath;
	private TextView sm_ygopath,sm_cdbpath;
	private TextView sm_cz1,sm_cz2;
	SharedPreferences sh;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shezhi_ez_management);
		csh();
	}
	
	private void csh(){
		tool=(Toolbar) findViewById(R.id.toolbar);
		sm_zdy_ygopath=(TextView) findViewById(R.id.sm_zdy_ygopath);
		sm_zdy_cdbpath=(TextView) findViewById(R.id.sm_zdy_cdbpath);
		sm_ygopath=(TextView) findViewById(R.id.sm_ygopath);
		sm_cdbpath=(TextView) findViewById(R.id.sm_cdbpath);
		sm_cz1=(TextView) findViewById(R.id.sm_cz1);
		sm_cz2=(TextView) findViewById(R.id.sm_cz2);
		
		gj=Gongju.getdx(this);
		sh=SharedPreferenceUtil.getSharePath();
		
		gj.cshToolbar(tool,"卡组管理设置");
		
		sm_ygopath.setText(SharedPreferenceUtil.getYgopath());
		sm_cdbpath.setText(SharedPreferenceUtil.getCdbpath());
		
		sm_zdy_cdbpath.setOnClickListener(this);
		sm_zdy_ygopath.setOnClickListener(this);
		sm_cz1.setOnClickListener(this);
		sm_cz2.setOnClickListener(this);
		
		
	}

	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.sm_zdy_ygopath:
				startActivityForResult(new Intent(ShezhiEzmanagement.this,NewWjxz.class).putExtra("lx",NewWjxz.TYPE_PATH).putExtra("path",Gongju.PATH_GEN),0);
				break;
			case R.id.sm_zdy_cdbpath:
				startActivityForResult(new Intent(ShezhiEzmanagement.this,NewWjxz.class).putExtra("lx",NewWjxz.TYPE_CDB).putExtra("path",Gongju.PATH_GEN),1);
				break;
			case R.id.sm_cz1:
				if(sh.edit().putString("ygopath",gj.PATH_GEN+"ygocore/").commit()){
					sm_ygopath.setText(gj.PATH_GEN+"ygocore/");
					gj.show("重置资源路径成功");
				}else{
					gj.show("重置资源路径失败");
				}
				break;
			case R.id.sm_cz2:
				String ps=Gongju.PATH_EZ_MANAGEMENT+"cards.cdb";
				if(new File(ps).exists()){
					if(FileUtil.delFile(ps)){
						fzas();
					}else{
						gj.show("重置数据库失败");
					}
				}else{
					fzas();
				}
				break;
		}
		// TODO: Implement this method
	}

	
		public void fzas(){
			new Thread(new Runnable(){

					@Override
					public void run()
					{
						//assets中文件名字
						String fileName = "cards.cdb";
						try
						{
							//拿到输入流
							InputStream in = getAssets().open(fileName);
							//打开输出流
							FileOutputStream out = new FileOutputStream(Gongju.PATH_EZ_MANAGEMENT+fileName);
							int len = -1 ;
							byte[] bytes = new byte[1024];
							//不断读取输入流
							while ((len = in.read(bytes)) != -1)
							{
								//写到输出流中
								out.write(bytes, 0, len);
							}
							out.close();
							in.close();
							
							if(sh.edit().putString("cdbpath",Gongju.PATH_EZ_MANAGEMENT+fileName).commit()){
								handle.sendEmptyMessage(1);
							}else{
								Message me=new Message();
								me.what=0;
								me.obj="未知原因";
								handle.sendMessage(me);
							}

						}
						catch (IOException e)
						{
							Message me=new Message();
							me.what=0;
							me.obj=e;
							handle.sendMessage(me);

						}
						// TODO: Implement this method
					}
					}).start();

					
		}
		
	
	
	Handler handle=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					gj.show("重置数据库失败,原因为"+msg.obj);
					break;
				case 1:
					sm_cdbpath.setText( gj.PATH_GEN+"ygocore/cards.cdb");
					gj.show("重置数据库成功");
					break;
			}
			}
		};
		
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		String path=data.getStringExtra("path");
		if(!path.equals("")){
			SharedPreferences sh=SharedPreferenceUtil.getSharePath();
			switch(requestCode){
				case 0:
					
					if(sh.edit().putString("ygopath",path).commit()){
						sm_ygopath.setText(path);
						gj.show("修改资源路径成功");
					}else{
						gj.show("修改资源路径失败");
					}
					break;
				case 1:
					if(sh.edit().putString("cdbpath",path).commit()){
						sm_cdbpath.setText(path);
						gj.show("自定义数据库成功");
					}else{
						gj.show("自定义数据库失败");
					}
					break;
			}
		}
		
		
	}

	
	
	
}
